package superminer.xinfang.com.ipreport.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.maning.updatelibrary.InstallUtils;


import superminer.xinfang.com.ipreport.util.DisplayUtil;
import superminer.xinfang.com.ipreport.view.FlikerProgressBar;
import superminer.xinfang.com.ipreport.R;

/**
 * Created by Administrator on 2017/11/21.
 */

public class DownLoadDialog extends Dialog {
    private FlikerProgressBar number_progress_bar;
    private Activity context;
    private String content;
    private String url;

    //下载后的APK的命名
    public static final String APK_NAME = "update";
    private static final String TAG = "InstallUtils";

    public DownLoadDialog(Activity context, int theme, String content, String url) {
        super(context, theme);
        this.context = context;
        this.content = content;
        this.url = url;
        this.setCanceledOnTouchOutside(false);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.dialog_down, null);
        setContentView(view, new ViewGroup.LayoutParams(DisplayUtil.getWidthPixels(context) / 2 + 150,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        number_progress_bar = (FlikerProgressBar) view.findViewById(R.id.number_progress_bar);
        createview();
    }


    private void createview() {

        new InstallUtils(context, url, APK_NAME, new InstallUtils.DownloadCallBack() {
            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(String path) {
                dismiss();
                Log.i(TAG, "InstallUtils---onComplete:" + path);

                /**
                 * 安装APK工具类
                 * @param context       上下文
                 * @param filePath      文件路径
                 * @param authorities   ---------Manifest中配置provider的authorities字段---------
                 * @param callBack      安装界面成功调起的回调
                 */
                InstallUtils.installAPK(context, path, context.getPackageName() + ".fileProvider", new InstallUtils.InstallCallBack() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(context, "正在安装程序", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFail(Exception e) {
                        Toast.makeText(context, "安装失败:" + e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void onLoading(long total, long current) {
                Log.i(TAG, "InstallUtils----onLoading:-----total:" + total + ",current:" + current);
                number_progress_bar.setProgress((int) (current * 100 / total));
            }

            @Override
            public void onFail(Exception e) {
                dismiss();
                Toast.makeText(context, "下载失败,请从新下载", Toast.LENGTH_SHORT).show();
                Log.i(TAG, "InstallUtils---onFail:" + e.getMessage());
            }

        }).downloadAPK();
    }

}
